import {
	Login
} from '@/api/login.js'
import {clearUser,getUser} from '@/utils/app.js'

const state = () => {
	return {
		user:getUser() || { token: '', username: ''}
	}
}
const getters = {

}
const mutations = {
	USER(state, val) {
		state.user = val
	},
	INIR(state,val){
		state.user = { token: '', username: ''}
	}
}
const actions = {
	login({
		state,
		commit
	}, params) {
		return new Promise(async (resolve, reject) => {
			try {
				const res = await Login(params)

				const {
					resCode,
					data,
					message
				} = res
				if (resCode === 0) {
					commit('USER', data)
					// token还是应该放在cook里面--永存存储 有对应的方法-我为了图方便就用token 
					localStorage.setItem('user', JSON.stringify(data))
					resolve(res)
				}
			} catch (e) {
				reject(e)
			}
		})
	},
	exit({commit}){
		clearUser()
		return true
	}
}



export default {
	namespaced: true,
	state,
	getters,
	mutations,
	actions
}
